<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-homestead" tabindex="-1"><a class="header-anchor" href="#laravel-homestead"><span>Laravel Homestead</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#installation-and-setup">安装与设置</a>
<ul>
<li><a href="#first-steps">第一步</a></li>
<li><a href="#configuring-homestead">配置 Homestead</a></li>
<li><a href="#launching-the-vagrant-box">启动 Vagrant Box</a></li>
<li><a href="#per-project-installation">根据项目安装</a></li>
<li><a href="#installing-optional-features">安装可选功能</a></li>
<li><a href="#aliases">别名</a></li>
</ul>
</li>
<li><a href="#daily-usage">日常使用</a>
<ul>
<li><a href="#accessing-homestead-globally">全局可用的 Homestead</a></li>
<li><a href="#connecting-via-ssh">通过 SSH 连接</a></li>
<li><a href="#connecting-to-databases">连接数据库</a></li>
<li><a href="#database-backups">数据库备份</a></li>
<li><a href="#database-snapshots">数据库快照</a></li>
<li><a href="#adding-additional-sites">添加其他站点</a></li>
<li><a href="#environment-variables">环境变量</a></li>
<li><a href="#configuring-cron-schedules">配置 Cron 调度器</a></li>
<li><a href="#configuring-mailhog">配置 Mailhog</a></li>
<li><a href="#configuring-minio">配置 Minio</a></li>
<li><a href="#ports">端口</a></li>
<li><a href="#sharing-your-environment">共享你的环境</a></li>
<li><a href="#multiple-php-versions">多个 PHP 版本</a></li>
<li><a href="#web-servers">Web 服务器</a></li>
<li><a href="#mail">邮件</a></li>
</ul>
</li>
<li><a href="#debugging-and-profiling">调试与分析</a>
<ul>
<li><a href="#debugging-web-requests">使用 Xdebug 调试 Web 请求</a></li>
<li><a href="#debugging-cli-applications">调试 CLI 应用程序</a></li>
<li><a href="#profiling-applications-with-blackfire">使用 Blackfire 分析应用程序</a></li>
</ul>
</li>
<li><a href="#network-interfaces">网络接口</a></li>
<li><a href="#extending-homestead">扩充 Homestead</a></li>
<li><a href="#updating-homestead">更新 Homestead</a></li>
<li><a href="#provider-specific-settings">提供器的特殊设置</a>
<ul>
<li><a href="#provider-specific-virtualbox">VirtualBox</a></li>
</ul>
</li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>Laravel 致力于让整个 PHP 开发体验变的更愉快，包括你的本地开发环境。<a href="https://www.vagrantup.com/" target="_blank" rel="noopener noreferrer">Vagrant</a> 提供了一种简单、优雅的方式来管理和配置虚拟主机。</p>
<p>Laravel Homestead 是一个官方预封装的 Vagrant box ，它为你提供了一个完美的开发环境，你不需要在本地机器安装 PHP、web 服务器和其他的服务器软件。你再也不用担心会弄乱你的操作系统了！Vagrant boxes 是一次性的。 如果出现问题， 你可以在几分钟内销毁并重新创建 box !</p>
<p>Homestead 可以运行在任何 Windows、Mac、或 Linux 系统， 它包括了 Nginx web 服务器，PHP 7.3、PHP 7.2、PHP 7.1、MySQL、PostgreSQL、Redis、Memcached、Node 以及你开发 Laravel 所需要的东西</p>
<blockquote>
<p>注意：如果你使用的是 Windows, 你可能需要通过 BIOS 来启用硬件虚拟化 (VT-x)。如果你在 Hyper-V 系统上使用 UEFI 你可能还需要禁用 Hyper-V 以便访问 VT-x。</p>
</blockquote>
<h3 id="内置软件" tabindex="-1"><a class="header-anchor" href="#内置软件"><span>内置软件</span></a></h3>
<p>- Ubuntu 18.04 - Git - PHP 7.3 - PHP 7.2 - PHP 7.1 - PHP 7.0 - PHP 5.6 - Nginx - MySQL - lmm for MySQL or MariaDB database snapshots - Sqlite3 - PostgreSQL - Composer - Node (With Yarn, Bower, Grunt, and Gulp) - Redis - Memcached - Beanstalkd - Mailhog - avahi - ngrok - Xdebug - XHProf / Tideways / XHGui - wp-cli</p>
<h3 id="可选软件" tabindex="-1"><a class="header-anchor" href="#可选软件"><span>可选软件</span></a></h3>
<p>- Apache - Blackfire - Cassandra - Chronograf - CouchDB - Crystal &amp; Lucky Framework - Docker - Elasticsearch - Gearman - Go - Grafana - InfluxDB - MariaDB - MinIO - MongoDB - MySQL 8 - Neo4j - Oh My Zsh - Open Resty - PM2 - Python - RabbitMQ - Solr - Webdriver &amp; Laravel Dusk Utilities</p>
<h2 id="安装与设置" tabindex="-1"><a class="header-anchor" href="#安装与设置"><span>安装与设置</span></a></h2>
<h3 id="第一步" tabindex="-1"><a class="header-anchor" href="#第一步"><span>第一步</span></a></h3>
<p>在你启动 Homestead 环境之前，你必须安装 <a href="https://www.virtualbox.org/wiki/Downloads" target="_blank" rel="noopener noreferrer">VirtualBox 6.x</a>，<a href="https://www.vmware.com/" target="_blank" rel="noopener noreferrer">VMWare</a>，<a href="https://www.parallels.com/products/desktop/" target="_blank" rel="noopener noreferrer">Parallels</a> 或 <a href="https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v" target="_blank" rel="noopener noreferrer">Hyper-V</a> 以及 <a href="https://www.vagrantup.com/downloads.html" target="_blank" rel="noopener noreferrer">Vagrant</a>。以上所有软件均针对不同的操作系统提供了易于使用的可视化安装包。</p>
<p>若要使用 VMware 提供器，你需要购买 VMware Fusion / Workstation 和 <a href="https://www.vagrantup.com/vmware" target="_blank" rel="noopener noreferrer">VMware Vagrant plug-in</a>。 虽然它不是免费的，但是 VMware 可以提供更快的共享文件夹性能。</p>
<p>若要使用 Parallels 提供器，你需要安装 <a href="https://github.com/Parallels/vagrant-parallels" target="_blank" rel="noopener noreferrer">Parallels Vagrant plug-in</a>。它是免费的。</p>
<p>由于受到 <a href="https://www.vagrantup.com/docs/hyperv/limitations.html" target="_blank" rel="noopener noreferrer">Vagrant 限制</a>，Hyper-V 提供程序会忽略所有的网络设置。</p>
<h4 id="安装-homestead-vagrant-box" tabindex="-1"><a class="header-anchor" href="#安装-homestead-vagrant-box"><span>安装 Homestead Vagrant Box</span></a></h4>
<p>一旦将 VirtualBox / VMware 和 Vagrant 都安装好之后，你可以在终端执行下面的命令将 <code v-pre>laravel/homestead box</code> 添加到 Vagrant 中安装。你可能需要几分钟的时间来下载 box，因为它取决于你的网络连接速度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">vagrant box add laravel<span class="token operator">/</span>homestead</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果这个命令失败了，请确保你安装的是最新版的 Vagrant。</p>
<blockquote>
<p>注意：Homestead 会定期发出「alpha」/「beta」box 用于测试，这可能会影响 <code v-pre>vagrant box add</code> 命令。如果你在运行 <code v-pre>vagrant box add</code> 时遇到问题，可以运行 <code v-pre>vagrant up</code> 命令，当 Vagrant 尝试启动虚拟机时，将下载正确的 box。</p>
</blockquote>
<h4 id="安装-homestead" tabindex="-1"><a class="header-anchor" href="#安装-homestead"><span>安装 Homestead</span></a></h4>
<p>你可以通过克隆代码来安装 Homestead。建议将代码克隆到你的「home」目录下的 Homestead 文件夹中，这样 Homestead box 就可以作为你的所有 Laravel 项目的主机：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">git <span class="token keyword">clone</span> https<span class="token punctuation">:</span><span class="token comment">//github.com/laravel/homestead.git ~/Homestead</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>因为 Homestead 的 <code v-pre>master</code> 分支并不是稳定的，你应该使用打过标签的稳定版本。您可以在 <a href="https://github.com/laravel/homestead/releases" target="_blank" rel="noopener noreferrer">GitHub Release Page</a> 上找到最新的稳定版。或者，你可以查看包含最新稳定版本的 <code v-pre>release</code> 分支：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">cd <span class="token operator">~</span><span class="token operator">/</span>Homestead</span>
<span class="line"></span>
<span class="line">git checkout release</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦克隆 Homestead 代码完成以后，在 Homestead 目录中使用 <code v-pre>bash init.sh</code> 命令来创建 <code v-pre>Homestead.yaml</code> 配置文件。<code v-pre>Homestead.yaml</code> 文件将被放在 Homestead 目录中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// Mac / Linux...</span></span>
<span class="line">bash init<span class="token operator">.</span>sh</span>
<span class="line"></span>
<span class="line"><span class="token comment">// Windows...</span></span>
<span class="line">init<span class="token operator">.</span>bat</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="配置-homestead" tabindex="-1"><a class="header-anchor" href="#配置-homestead"><span>配置 Homestead</span></a></h3>
<h4 id="设置你的提供器" tabindex="-1"><a class="header-anchor" href="#设置你的提供器"><span>设置你的提供器</span></a></h4>
<p><code v-pre>Homestead.yaml</code> 文件中的 <code v-pre>provider</code> 参数决定了你用的是哪一个 Vagrant 提供器 : <code v-pre>virtualbox</code>, <code v-pre>vmware_fusion</code>, <code v-pre>vmware_workstation</code>, <code v-pre>parallels</code> 以及 <code v-pre>hyperv</code>。你可以根据你的喜好来设置他们：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">provider<span class="token punctuation">:</span> virtualbox</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="配置共享文件夹" tabindex="-1"><a class="header-anchor" href="#配置共享文件夹"><span>配置共享文件夹</span></a></h4>
<p><code v-pre>Homestead.yaml</code> 文件的 <code v-pre>folders</code> 属性里列出了所有与 Homestead 环境共享的文件夹。这些文件夹中的文件如果发生变更，它们会保持本地机器与 Homestead 环境之间同步。你可以根据需要配置多个共享文件夹：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">folders<span class="token punctuation">:</span></span>
<span class="line">    <span class="token operator">-</span> map<span class="token punctuation">:</span> <span class="token operator">~</span><span class="token operator">/</span>code<span class="token operator">/</span>project1</span>
<span class="line">      to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>project1</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：Windows用户不应使用 <code v-pre>〜/</code> 路径语法，而应使用其项目的完整路径，例如 <code v-pre>C:\Users\user\Code\project1</code>。</p>
</blockquote>
<p>你应始终将各个项目映射到自己的文件夹映射，而不是映射整个 <code v-pre>〜/ code</code> 文件夹。映射文件夹时，虚拟机必须跟踪文件夹中 <em>每个</em> 文件的所有磁盘 IO。如果文件夹中有大量文件，则会导致性能问题。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">folders<span class="token punctuation">:</span></span>
<span class="line">    <span class="token operator">-</span> map<span class="token punctuation">:</span> <span class="token operator">~</span><span class="token operator">/</span>code<span class="token operator">/</span>project1</span>
<span class="line">      to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>project1</span>
<span class="line"></span>
<span class="line">    <span class="token operator">-</span> map<span class="token punctuation">:</span> <span class="token operator">~</span><span class="token operator">/</span>code<span class="token operator">/</span>project2</span>
<span class="line">      to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>project2</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：使用Homestead时，不应该挂载到 <code v-pre>.</code>（当前目录）。这会导致 Vagrant 不将当前文件夹映射到 <code v-pre>/vagrant</code>，并且会破坏可选功能并在配置时导致意外结果。</p>
</blockquote>
<p>若要开启 <a href="https://www.vagrantup.com/docs/synced-folders/nfs.html" target="_blank" rel="noopener noreferrer">NFS</a>，你只需要在共享的文件夹配置中添加一个简单的标志：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">folders<span class="token punctuation">:</span></span>
<span class="line">    <span class="token operator">-</span> map<span class="token punctuation">:</span> <span class="token operator">~</span><span class="token operator">/</span>code<span class="token operator">/</span>project1</span>
<span class="line">      to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>project1</span>
<span class="line">      type<span class="token punctuation">:</span> <span class="token string double-quoted-string">"nfs"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：当使用 NFS 时，你最好使用 <a href="https://github.com/winnfsd/vagrant-winnfsd" target="_blank" rel="noopener noreferrer">vagrant-winnfsd</a> 扩展插件。这个插件会替你处理 Homestead box 中的文件或目录权限的问题。</p>
</blockquote>
<p>你也可以通过在 <code v-pre>options</code> 中列出 Vagrant 的 <a href="https://www.vagrantup.com/docs/synced-folders/basic_usage.html" target="_blank" rel="noopener noreferrer">同步文件夹</a> 支持的任何选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">folders<span class="token punctuation">:</span></span>
<span class="line">    <span class="token operator">-</span> map<span class="token punctuation">:</span> <span class="token operator">~</span><span class="token operator">/</span>code<span class="token operator">/</span>project1</span>
<span class="line">      to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>project1</span>
<span class="line">      type<span class="token punctuation">:</span> <span class="token string double-quoted-string">"rsync"</span></span>
<span class="line">      options<span class="token punctuation">:</span></span>
<span class="line">          rsync__args<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string double-quoted-string">"--verbose"</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"--archive"</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"--delete"</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"-zz"</span><span class="token punctuation">]</span></span>
<span class="line">          rsync__exclude<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string double-quoted-string">"node_modules"</span><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="配置-nginx-站点" tabindex="-1"><a class="header-anchor" href="#配置-nginx-站点"><span>配置 Nginx 站点</span></a></h4>
<p>对 Nginx 不熟悉? 没关系。 <code v-pre>sites</code> 功能可以让你在 Homestead 上轻松的映射一个”域名”到一个文件夹。在 <code v-pre>Homestead.yaml</code> 文件中包含了一个简单的站点配置示例。同样，您可以根据需要为您的 Homestead 环境添加很多的站点。Homestead 可以为你正在开发的每个 Laravel 项目提供一个方便的虚拟化环境：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">sites<span class="token punctuation">:</span></span>
<span class="line">    <span class="token operator">-</span> map<span class="token punctuation">:</span> homestead<span class="token operator">.</span>test</span>
<span class="line">      to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>project1<span class="token operator">/</span><span class="token keyword">public</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你在 Homestead 虚拟机启动后更改了 <code v-pre>sites</code> 选项，你需要再次运行 <code v-pre>vagrant reload --provision</code> 命令去更新虚拟机上的 Nginx 配置。</p>
<blockquote>
<p>注意：Homestead 脚本被设计为尽可能保证操作的幂等，不过，如果你在 provison 过程中还是出现问题，则需要通过 <code v-pre>vagrant destroy &amp;&amp; vagrant up</code> 命令来销毁并重构虚拟机。</p>
</blockquote>
<h4 id="主机名解析" tabindex="-1"><a class="header-anchor" href="#主机名解析"><span>主机名解析</span></a></h4>
<p>Homestead 通过 <code v-pre>mDNS</code> 发布主机名来自动解析主机。如果你在 <code v-pre>Homestead.yaml</code> 中设置了 <code v-pre>hostname: homestead</code>，则该主机会以 <code v-pre>homestead.local</code> 的形式生效。MacOS、iOS 和 Linux 桌面系统默认提供了对 <code v-pre>mDNS</code> 的支持，Windows 需要安装 <a href="https://support.apple.com/kb/DL999?viewlocale=zh_CN&amp;locale=en_US" target="_blank" rel="noopener noreferrer">Bonjour Print Services for Windows</a> 使其生效。</p>
<p>自动主机名在每个项目独立安装的 Homestead 中工作最好，如果你在一个 Homestead 实例上托管了多个站点，可以在宿主机上将站点「域名」添加到 <code v-pre>hosts</code> 文件中做域名映射，<code v-pre>hosts</code> 文件会将针对 Homestead 站点的请求重定向到 Homestead 虚拟机。在 Mac 和 Linux 系统中，该文件位于 <code v-pre>/etc/hosts</code>，在 Windows 系统中，该文件位于 <code v-pre>C:\Windows\System32\drivers\etc\hosts</code>。我们以 <code v-pre>homestead.test</code> 域名映射为例，添加到 <code v-pre>hosts</code> 文件的记录如下所示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token number">192.168</span><span class="token number">.10</span><span class="token number">.10</span>  homestead<span class="token operator">.</span>test</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>确保 IP 地址和你的 <code v-pre>Homestead.yaml</code> 文件中的配置项一致，一旦你将域名添加到 <code v-pre>hosts</code> 文件中并启动 Homestead，就可以在浏览器中通过该域名访问站点了：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">http<span class="token punctuation">:</span><span class="token comment">//homestead.test</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="启动-vagrant-盒子" tabindex="-1"><a class="header-anchor" href="#启动-vagrant-盒子"><span>启动 Vagrant 盒子</span></a></h3>
<p>根据你的需求编辑完成 <code v-pre>Homestead.yaml</code>，在你的 Homestead 文件夹中运行 <code v-pre>vagrant up</code> 命令。Vagrant 将启动虚拟机并自动配置你的共享文件夹和 Nginx 站点。</p>
<p>若要删除虚拟机，只需要运行 <code v-pre>vagrant destroy --force</code> 命令。</p>
<h3 id="根据项目安装" tabindex="-1"><a class="header-anchor" href="#根据项目安装"><span>根据项目安装</span></a></h3>
<p>除了全局安装 Homestead 并且在所有项目共享相同的 Homestead box 之外， 你可以为每个项目配置 Homestead 实例。 通过在项目下创建 <code v-pre>Vagrantfile</code>，其他的项目成员只需运行 <code v-pre>vagrant up</code> 就能拥有相同的开发环境。</p>
<p>要将 Homestead 直接安装到项目中，需要使用 Composer 命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> laravel<span class="token operator">/</span>homestead <span class="token operator">--</span>dev</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Homestead 安装之后， 使用 <code v-pre>make</code> 命令在项目根目录中生成 <code v-pre>Vagrantfile</code> 和 <code v-pre>Homestead.yaml</code> 文件。 <code v-pre>make</code> 命令会自动配置 <code v-pre>Homestead.yaml</code> 文件中 <code v-pre>sites</code> 和 <code v-pre>folders</code> 指令。</p>
<p>Mac / Linux:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php vendor<span class="token operator">/</span>bin<span class="token operator">/</span>homestead make</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Windows:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">vendor\\bin\\homestead make</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，在命令行中运行 <code v-pre>vagrant up</code> 命令，然后在浏览器中访问 <code v-pre>http://homestead.test</code> 。 记住，如果你没有使用自动 <a href="#hostname-resolution">主机名解析</a>，在访问之前你仍然需要在 <code v-pre>/etc/hosts</code> 文件中添加你的 <code v-pre>homestead.test</code> 记录或者其他的域名。</p>
<h3 id="安装可选功能" tabindex="-1"><a class="header-anchor" href="#安装可选功能"><span>安装可选功能</span></a></h3>
<p>安装可选功能可以在 Homestead 配置文件 <code v-pre>Homestead.yaml</code> 中使用 <code v-pre>features</code> 设置来实现，大部分功能只需要通过布尔值设置启用或者禁用来选择安装或者不安装，有些功能还允许配置多个选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">features<span class="token punctuation">:</span></span>
<span class="line">    <span class="token operator">-</span> blackfire<span class="token punctuation">:</span></span>
<span class="line">        server_id<span class="token punctuation">:</span> <span class="token string double-quoted-string">"server_id"</span></span>
<span class="line">        server_token<span class="token punctuation">:</span> <span class="token string double-quoted-string">"server_value"</span></span>
<span class="line">        client_id<span class="token punctuation">:</span> <span class="token string double-quoted-string">"client_id"</span></span>
<span class="line">        client_token<span class="token punctuation">:</span> <span class="token string double-quoted-string">"client_value"</span></span>
<span class="line">    <span class="token operator">-</span> cassandra<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> chronograf<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> couchdb<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> crystal<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> docker<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> elasticsearch<span class="token punctuation">:</span></span>
<span class="line">        version<span class="token punctuation">:</span> <span class="token number">7</span></span>
<span class="line">    <span class="token operator">-</span> gearman<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> golang<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> grafana<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> influxdb<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> mariadb<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> minio<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> mongodb<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> mysql8<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> neo4j<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> ohmyzsh<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> openresty<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> pm2<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> python<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> rabbitmq<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> solr<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line">    <span class="token operator">-</span> webdriver<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="mariadb" tabindex="-1"><a class="header-anchor" href="#mariadb"><span>MariaDB</span></a></h4>
<p>启用 MariaDB 将会移除 MySQL 并安装 MariaDB。MariaDB 是 MySQL 的替代品，完全兼容 MySQL，所以在应用的数据库配置中你仍然可以使用 <code v-pre>mysql</code> 驱动。</p>
<h4 id="mongodb" tabindex="-1"><a class="header-anchor" href="#mongodb"><span>MongoDB</span></a></h4>
<p>默认安装的 MongoDB 将会设置数据库用户名为 <code v-pre>homestead</code> 及对应的密码为 <code v-pre>secret</code>。</p>
<h4 id="elasticsearch" tabindex="-1"><a class="header-anchor" href="#elasticsearch"><span>Elasticsearch</span></a></h4>
<p>如果你要安装 Elasticsearch，你可以在 <code v-pre>Homestead.yaml</code> 文件中添加 <code v-pre>elasticsearch</code> 选项并指定支持的版本号。可以仅包含主版本，也可以是某个具体的版本号（major.minor.patch）。默认安装会创建一个名为「homestead」的集群。 注意永远不要赋予 Elasticsearch 超过操作系统一半的内存，因此请保证你的 Homestead 至少分配了两倍于 Elasticsearch 的内存。</p>
<blockquote>
<p>提示：你可以查看 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current" target="_blank" rel="noopener noreferrer">Elasticsearch documentation</a> 文档学习如何自定义你的配置。</p>
</blockquote>
<h4 id="neo4j" tabindex="-1"><a class="header-anchor" href="#neo4j"><span>Neo4j</span></a></h4>
<p>默认安装的 Neo4j 会设置数据库用户名为 <code v-pre>homestead</code> 及对应的密码 <code v-pre>secret</code>。要查看 Neo4j，可以在浏览器中访问 <code v-pre>http://homestead.test:7474</code>，Neo4j 对外提供了三个端口：<code v-pre>7687</code>（Bolt）、<code v-pre>7474</code>（HTTP）、<code v-pre>7473</code>（HTTPS） 用于从客户端访问。</p>
<h3 id="别名" tabindex="-1"><a class="header-anchor" href="#别名"><span>别名</span></a></h3>
<p>你可以在 Homestead 目录下通过编辑 <code v-pre>aliases</code> 文件为 Homestead 机器添加 Bash 别名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">alias c<span class="token operator">=</span><span class="token string single-quoted-string">'clear'</span></span>
<span class="line">alias <span class="token operator">.</span><span class="token operator">.=</span><span class="token string single-quoted-string">'cd ..'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>更新完 <code v-pre>aliases</code> 文件后，需要通过 <code v-pre>vagrant reload --provision</code> 命令重启 Homestead 机器，以确保新的别名在机器上生效。</p>
<h2 id="日常使用" tabindex="-1"><a class="header-anchor" href="#日常使用"><span>日常使用</span></a></h2>
<h3 id="全局访问-homestead" tabindex="-1"><a class="header-anchor" href="#全局访问-homestead"><span>全局访问 Homestead</span></a></h3>
<p>有时你可能想在文件系统的任意路径都能运行 <code v-pre>vagrant up</code> 命令启动 Homestead 虚拟机。在 Mac / Linux 系统上可以在 Bash 配置文件（ <code v-pre>~/.bash_profile</code> ）中添加 Bash 函数 。在 Windows 上， 你可以添加「批处理」文件到 <code v-pre>PATH</code> 。这些脚本允许你在系统的任何位置运行 Vagrant 命令并自动将该命令指向 Homestead 的安装路径。</p>
<h4 id="mac-linux" tabindex="-1"><a class="header-anchor" href="#mac-linux"><span>Mac / Linux</span></a></h4>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">function</span> <span class="token function-definition function">homestead</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token punctuation">(</span> cd <span class="token operator">~</span><span class="token operator">/</span>Homestead <span class="token operator">&amp;&amp;</span> <span class="token class-name type-declaration">vagrant</span> $<span class="token operator">*</span> <span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>确保将该函数中的 <code v-pre>~/Homestead</code> 路径调整为实际的 Homestead 安装路径。这样你就可以在系统的任意位置运行 <code v-pre>homestead up</code> 或 <code v-pre>homestead ssh</code> 等命令。</p>
<h4 id="windows" tabindex="-1"><a class="header-anchor" href="#windows"><span>Windows</span></a></h4>
<p>在系统的任意位置创建一个批处理文件 <code v-pre>homestead.bat</code> ，内容如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token keyword">echo</span> off</span>
<span class="line"></span>
<span class="line">set cwd<span class="token operator">=</span><span class="token operator">%</span>cd<span class="token operator">%</span></span>
<span class="line">set homesteadVagrant<span class="token operator">=</span><span class="token constant">C</span><span class="token punctuation">:</span>\Homestead</span>
<span class="line"></span>
<span class="line">cd <span class="token operator">/</span>d <span class="token operator">%</span>homesteadVagrant<span class="token operator">%</span> <span class="token operator">&amp;&amp;</span> vagrant <span class="token operator">%</span><span class="token operator">*</span></span>
<span class="line">cd <span class="token operator">/</span>d <span class="token operator">%</span>cwd<span class="token operator">%</span></span>
<span class="line"></span>
<span class="line">set cwd<span class="token operator">=</span></span>
<span class="line">set homesteadVagrant<span class="token operator">=</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一定要将脚本中 <code v-pre>C:\Homestead</code> 路径调整为 Homestead 的实际安装路径。创建文件后，将文件路径添加到 <code v-pre>PATH</code> 。然后你就可以在系统任意位置运行 <code v-pre>homestead up</code> 或 <code v-pre>homestead ssh</code> 等命令。</p>
<h3 id="通过-ssh-连接" tabindex="-1"><a class="header-anchor" href="#通过-ssh-连接"><span>通过 SSH 连接</span></a></h3>
<p>你可以通过在 Homestead 目录下运行 <code v-pre>vagrant ssh</code> 终端命令以 SSH 的方式连接到虚拟机。</p>
<p>但是，你可能需要频繁连接 Homestead 虚拟机，因此请考虑将上面的「函数」添加到主机以便快速地以 SSH 方式连接到 Homestead 虚拟机。</p>
<h3 id="连接数据库" tabindex="-1"><a class="header-anchor" href="#连接数据库"><span>连接数据库</span></a></h3>
<p><code v-pre>homestead</code> 默认已经在虚拟机中为 MySQL 和 PostgreSQL 数据库做好了配置。要从主机的数据库客户端连接到 MySQL 或 PostgreSQL ，应该连接到 <code v-pre>127.0.0.1</code> ，端口 <code v-pre>33060</code> （MySQL） 或 <code v-pre>54320</code> （PostgreSQL）。用户名和密码分别是 <code v-pre>homestead</code> / <code v-pre>secret</code> 。</p>
<blockquote>
<p>注意：只有从本地连接 Homestead 的数据库时才能使用这些非标准的端口。而 Laravel <em>在虚拟机中</em> 运行时还是应该使用默认的 3306 和 5432 端口进行数据库连接。</p>
</blockquote>
<h3 id="数据库备份" tabindex="-1"><a class="header-anchor" href="#数据库备份"><span>数据库备份</span></a></h3>
<p>当你的 Vagrant 盒子销毁时，Homestead 能自动备份你的数据库。为了利用这个功能，你必须使用 Vagrant 2.1.0 或者更高的版本。或者，如果你使用一个旧版本的 Vagrant，你必须安装 <code v-pre>vagrant-triggers</code> 插件。要开启自动数据库备份，需添加如下的行到你的 <code v-pre>Homestead.yaml</code> 文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">backup<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>一旦配置了，当 <code v-pre>vagrant destroy</code> 命令被执行时，Homestead 将导出你的数据库到 <code v-pre>mysql_backup</code> 和 <code v-pre>postgres_backup</code> 目录。如果你使用 <a href="#per-project-installation">每个项目安装</a> 方法，则可以在克隆的 Homestead 或者在你的项目根目录中找到这些目录。</p>
<h3 id="数据库快照" tabindex="-1"><a class="header-anchor" href="#数据库快照"><span>数据库快照</span></a></h3>
<p>Homestead 支持保存 MySQL 和 MariaDB 数据库的状态并通过 <a href="https://github.com/Lullabot/lmm" target="_blank" rel="noopener noreferrer">Logical MySQL Manager</a>在不同状态间进行切换。例如，假设你的站点数据库有几个 G 的数据量，你可以导入这个数据库并保存一份快照，在本地工作一段时间后有可能会创建一些新的测试内容，你可以通过快照快速恢复到最初的状态。</p>
<p>在底层，LMM 使用了 LVM 的支持写时复制的瘦快照功能，这意味着当修改表中某条记录时，只会将你所做的更改写入磁盘，从而在恢复期节省大量时间和磁盘空间。</p>
<p>由于 <code v-pre>lmm</code> 会与 LVM 进行交互，所以必须以 <code v-pre>root</code> 身份运行。要了解所有命令，可以在 Homestead 虚拟机中通过 <code v-pre>sudo lmm</code> 查看。常见的工作流会是这样：</p>
<ol>
<li>导入数据库到 lmm 默认的 <code v-pre>master</code> 分支。</li>
<li>运行 <code v-pre>sudo lmm branch prod-YYYY-MM-DD</code> 保存尚未做任何修改的数据库快照。</li>
<li>修改数据库记录。</li>
<li>运行 <code v-pre>sudo lmm merge prod-YYYY-MM-D</code> 撤销所有修改。</li>
<li>运行 <code v-pre>sudo lmm delete &lt;branch&gt;</code> 删除不需要的分支。</li>
</ol>
<h3 id="添加额外的站点" tabindex="-1"><a class="header-anchor" href="#添加额外的站点"><span>添加额外的站点</span></a></h3>
<p>一旦你的 Homestead 环境已配置并运行，你可以为你的 Laravel 应用添加额外的 Nginx 站点。你可能希望在单个 Homestead 环境运行多个 Laravel 安装。要添加额外的站点，到 <code v-pre>Homestead.yaml</code> 文件中添加站点：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">sites<span class="token punctuation">:</span></span>
<span class="line">    <span class="token operator">-</span> map<span class="token punctuation">:</span> homestead<span class="token operator">.</span>test</span>
<span class="line">      to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>project1<span class="token operator">/</span><span class="token keyword">public</span></span>
<span class="line">    <span class="token operator">-</span> map<span class="token punctuation">:</span> another<span class="token operator">.</span>test</span>
<span class="line">      to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>project2<span class="token operator">/</span><span class="token keyword">public</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果 Vagrant 不能自动管理你的『hosts』文件，你可能还需要添加新的站点到该文件中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token number">192.168</span><span class="token number">.10</span><span class="token number">.10</span>  homestead<span class="token operator">.</span>test</span>
<span class="line"><span class="token number">192.168</span><span class="token number">.10</span><span class="token number">.10</span>  another<span class="token operator">.</span>test</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦这个站点被添加，从你的 Homestead 目录运行 <code v-pre>vagrant reload --provision</code> 命令。</p>
<h4 id="站点类型" tabindex="-1"><a class="header-anchor" href="#站点类型"><span>站点类型</span></a></h4>
<p>Homestead 支持多种类型的站点，允许你轻松运行不是基于 Laravel 的项目。例如，我们可以使用 <code v-pre>symfony2</code> 站点类型轻松地添加一个 Symfony 应用到 Homestead：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">sites<span class="token punctuation">:</span></span>
<span class="line">    <span class="token operator">-</span> map<span class="token punctuation">:</span> symfony2<span class="token operator">.</span>test</span>
<span class="line">      to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>my<span class="token operator">-</span>symfony<span class="token operator">-</span>project<span class="token operator">/</span>web</span>
<span class="line">      type<span class="token punctuation">:</span> <span class="token string double-quoted-string">"symfony2"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可用的站点类型是：<code v-pre>apache</code>，<code v-pre>apigility</code>，<code v-pre>expressive</code>，<code v-pre>laravel</code>（默认），<code v-pre>proxy</code>，<code v-pre>silverstripe</code>，<code v-pre>statamic</code>，<code v-pre>symfony2</code>，<code v-pre>symfony4</code>，和 <code v-pre>zf</code>。</p>
<h4 id="站点参数" tabindex="-1"><a class="header-anchor" href="#站点参数"><span>站点参数</span></a></h4>
<p>你可以通过 <code v-pre>params</code> 站点指令添加额外的 Nginx <code v-pre>fastcgi_param</code> 值到你的站点。例如，我们可以添加一个 <code v-pre>FOO</code> 参数，对应参数值是 <code v-pre>BAR</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">sites<span class="token punctuation">:</span></span>
<span class="line">    <span class="token operator">-</span> map<span class="token punctuation">:</span> homestead<span class="token operator">.</span>test</span>
<span class="line">      to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>project1<span class="token operator">/</span><span class="token keyword">public</span></span>
<span class="line">      params<span class="token punctuation">:</span></span>
<span class="line">          <span class="token operator">-</span> key<span class="token punctuation">:</span> <span class="token constant">FOO</span></span>
<span class="line">            value<span class="token punctuation">:</span> <span class="token constant">BAR</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="环境变量" tabindex="-1"><a class="header-anchor" href="#环境变量"><span>环境变量</span></a></h3>
<p>你能通过添加如下的值到 <code v-pre>Homestead.yaml</code> 文件来设置全局环境变量：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">variables<span class="token punctuation">:</span></span>
<span class="line">    <span class="token operator">-</span> key<span class="token punctuation">:</span> <span class="token constant">APP_ENV</span></span>
<span class="line">      value<span class="token punctuation">:</span> local</span>
<span class="line">    <span class="token operator">-</span> key<span class="token punctuation">:</span> <span class="token constant">FOO</span></span>
<span class="line">      value<span class="token punctuation">:</span> bar</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>更新 <code v-pre>Homestead.yaml</code> 文件之后，确保通过运行 <code v-pre>vagrant reload --provision</code> 命令重新配置机器。这个将更新所有安装 的 PHP 版本的 PHP-FPM 配置并为 <code v-pre>vagrant</code> 用户更新环境。</p>
<h3 id="配置-cron-定时计划" tabindex="-1"><a class="header-anchor" href="#配置-cron-定时计划"><span>配置 Cron 定时计划</span></a></h3>
<p>Laravel 提供了一种 <a href="https://learnku.com/docs/laravel/6.x/scheduling" target="_blank" rel="noopener noreferrer">定时计划作业</a> 的方式，只需每分钟运行一次 <code v-pre>schedule:run</code> Artisan 命令即可。<code v-pre>schedule:run</code> 命令将检查在你在 <code v-pre>App\Console\Kernel</code> 类中定义的计划来决定运行哪个作业。</p>
<p>如果想要为某个 Homestead 站点运行 <code v-pre>schedule:run</code> 命令，需要在定义站点时设置 <code v-pre>schedule</code> 为 <code v-pre>true</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">sites<span class="token punctuation">:</span></span>
<span class="line">    <span class="token operator">-</span> map<span class="token punctuation">:</span> homestead<span class="token operator">.</span>test</span>
<span class="line">      to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>project1<span class="token operator">/</span><span class="token keyword">public</span></span>
<span class="line">      schedule<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>站点的 Cron 作业将被定义在虚拟机的 <code v-pre>/etc/cron.d</code> 目录中。</p>
<h3 id="配置-mailhog" tabindex="-1"><a class="header-anchor" href="#配置-mailhog"><span>配置 Mailhog</span></a></h3>
<p>Mailhog 允许你轻松地捕获外发的电子邮件并进行检查它，而实际上没有将发送邮件给收件人。开始使用时，使用以下的邮件设置更新你的<code v-pre>.env</code> 文件：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token assign-left variable">MAIL_DRIVER</span><span class="token operator">=</span>smtp</span>
<span class="line"><span class="token assign-left variable">MAIL_HOST</span><span class="token operator">=</span>localhost</span>
<span class="line"><span class="token assign-left variable">MAIL_PORT</span><span class="token operator">=</span><span class="token number">1025</span></span>
<span class="line"><span class="token assign-left variable">MAIL_USERNAME</span><span class="token operator">=</span>null</span>
<span class="line"><span class="token assign-left variable">MAIL_PASSWORD</span><span class="token operator">=</span>null</span>
<span class="line"><span class="token assign-left variable">MAIL_ENCRYPTION</span><span class="token operator">=</span>null</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦 Mailhog 被配置，你可以在 <code v-pre>http://localhost:8025</code> 上访问 Mailhog 控制面板。</p>
<h3 id="配置-minio" tabindex="-1"><a class="header-anchor" href="#配置-minio"><span>配置 Minio</span></a></h3>
<p>Minio 是一个开源的对象存储服务器，具有与 Amazon S3 兼容的 API。要安装 Minio，用以下的配置选项更新 <code v-pre>Homestead.yaml</code> 文件中的 <a href="#installing-optional-features">features</a> 项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">minio: <span class="token boolean">true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>默认情况下，Minio 在端口 <code v-pre>9600</code> 上是可用的。你可以通过访问 <code v-pre>http://homestead:9600</code> 进入Minio 控制面板。默认的 <code v-pre>access key</code> 是 <code v-pre>homestead</code>，默认的 <code v-pre>secret key</code> 是 <code v-pre>secretkey</code>，访问 Minio 时，需要使用 <code v-pre>us-east-1</code> 区域。</p>
<p>为了使用 Minio，你将需要在你的 <code v-pre>config/filesystems.php</code> 配置文件中调整 S3 磁盘配置。你将需要添加 <code v-pre>use_path_style_endpoint</code> 选项到磁盘配置，并将 <code v-pre>url</code> 键更改为 <code v-pre>endpoint</code>：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token string">'s3'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string">'driver'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'s3'</span>,</span>
<span class="line">    <span class="token string">'key'</span> <span class="token operator">=</span><span class="token operator">></span> env<span class="token punctuation">(</span><span class="token string">'AWS_ACCESS_KEY_ID'</span><span class="token punctuation">)</span>,</span>
<span class="line">    <span class="token string">'secret'</span> <span class="token operator">=</span><span class="token operator">></span> env<span class="token punctuation">(</span><span class="token string">'AWS_SECRET_ACCESS_KEY'</span><span class="token punctuation">)</span>,</span>
<span class="line">    <span class="token string">'region'</span> <span class="token operator">=</span><span class="token operator">></span> env<span class="token punctuation">(</span><span class="token string">'AWS_DEFAULT_REGION'</span><span class="token punctuation">)</span>,</span>
<span class="line">    <span class="token string">'bucket'</span> <span class="token operator">=</span><span class="token operator">></span> env<span class="token punctuation">(</span><span class="token string">'AWS_BUCKET'</span><span class="token punctuation">)</span>,</span>
<span class="line">    <span class="token string">'endpoint'</span> <span class="token operator">=</span><span class="token operator">></span> env<span class="token punctuation">(</span><span class="token string">'AWS_URL'</span><span class="token punctuation">)</span>,</span>
<span class="line">    <span class="token string">'use_path_style_endpoint'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token boolean">true</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>最后，确保你的 <code v-pre>.env</code> 文件有如下的选项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token assign-left variable">AWS_ACCESS_KEY_ID</span><span class="token operator">=</span>homestead</span>
<span class="line"><span class="token assign-left variable">AWS_SECRET_ACCESS_KEY</span><span class="token operator">=</span>secretkey</span>
<span class="line"><span class="token assign-left variable">AWS_DEFAULT_REGION</span><span class="token operator">=</span>us-east-1</span>
<span class="line"><span class="token assign-left variable">AWS_URL</span><span class="token operator">=</span>http://homestead:9600</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>为了配置 buckets，要添加 <code v-pre>buckets</code> 指令到 Homestead 配置文件：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">buckets:</span>
<span class="line">    - name: your-bucket</span>
<span class="line">      policy: public</span>
<span class="line">    - name: your-private-bucket</span>
<span class="line">      policy: none</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>支持的 <code v-pre>policy</code> 值包括：<code v-pre>none</code>，<code v-pre>download</code>，<code v-pre>upload</code> 和 <code v-pre>public</code>。</p>
<h3 id="端口" tabindex="-1"><a class="header-anchor" href="#端口"><span>端口</span></a></h3>
<p>默认情况下，如下的端口将转发到你的 Homestead 环境：</p>
<ul>
<li><strong>SSH</strong>：2222 -&gt; 转发到 22</li>
<li><strong>ngrok UI</strong>：4040 -&gt; 转发到 4040</li>
<li><strong>HTTP</strong>：8000 -&gt; 转发到 80</li>
<li><strong>HTTPS</strong>：44300 -&gt; 转发到 443</li>
<li><strong>MySQL</strong>：33060 -&gt; 转发到 3306</li>
<li><strong>PostgreSQL</strong>：54320 -&gt; 转发到 5432</li>
<li><strong>MongoDB</strong>：27017 -&gt; 转发到 27017</li>
<li><strong>Mailhog</strong>：8025 -&gt; 转发到 8025</li>
<li><strong>Minio</strong>：9600 -&gt; 转发到 9600</li>
</ul>
<h4 id="转发额外的端口" tabindex="-1"><a class="header-anchor" href="#转发额外的端口"><span>转发额外的端口</span></a></h4>
<p>如果你愿意，你可以转发额外的端口到 Vagrant 盒子，同时也指定他们的协议：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">ports:</span>
<span class="line">    - send: <span class="token number">50000</span></span>
<span class="line">      to: <span class="token number">5000</span></span>
<span class="line">    - send: <span class="token number">7777</span></span>
<span class="line">      to: <span class="token number">777</span></span>
<span class="line">      protocol: udp</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="共享你的环境" tabindex="-1"><a class="header-anchor" href="#共享你的环境"><span>共享你的环境</span></a></h3>
<p>有时你希望跟同事或客户共享你当前工作的内容。Vagrant 有内置的方法通过 <code v-pre>vagrant share</code> 去支持这个；然而，如果在 <code v-pre>Homestead.yaml</code> 文件中有多个站点配置，这个将不会工作。</p>
<p>为了解决这个问题，Homestead 包含它自己的 <code v-pre>share</code> 命令。开始时，SSH 通过 <code v-pre>vagrant ssh</code> 进入你的 Homestead 机器并运行 <code v-pre>share homestead.test</code>。这个将从你的 <code v-pre>Homestead.yaml</code> 配置文件中分享 <code v-pre>homestead.test</code> 站点。当然，你可以将任何其它配置的站点替换为 <code v-pre>homestead.test</code>：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">share homestead.test</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>运行此命令之后，你将看到一个 Ngrok 屏幕出现，其中包含了活动日志和共享站点的可访问的 URLs。如果你想指定一个自定义的区域，子域，或者其它 Ngrok 运行时选项，你可以添加他们到你的 <code v-pre>share</code> 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">share homestead.test <span class="token parameter variable">-region</span><span class="token operator">=</span>eu <span class="token parameter variable">-subdomain</span><span class="token operator">=</span>laravel</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>注意：记住，Vagrant 本质上是不安全的，并且在运行 <code v-pre>share</code> 命令时将虚拟机显露到互联网。</p>
</blockquote>
<h3 id="多版本-php" tabindex="-1"><a class="header-anchor" href="#多版本-php"><span>多版本 PHP</span></a></h3>
<p>Homestead 6 在同一个虚拟机上引入了对多个 PHP 版本的支持。你可以在你的 <code v-pre>Homestead.yaml</code> 文件中指定要用于给定站点的 PHP 版本。可用的 PHP 版本是：『5.6』，『7.0』，『7.1』，『7.2』和『7.3』（默认）：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">sites:</span>
<span class="line">    - map: homestead.test</span>
<span class="line">      to: /home/vagrant/code/my-project/public</span>
<span class="line">      php: <span class="token string">"7.1"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>另外，你可以通过 CLI 使用任何支持的 PHP 版本：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php5.6 artisan list</span>
<span class="line">php7.0 artisan list</span>
<span class="line">php7.1 artisan list</span>
<span class="line">php7.2 artisan list</span>
<span class="line">php7.3 artisan list</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>还可以在虚拟机中通过如下这些命令来更新默认的 CLI 版本：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php56</span>
<span class="line">php70</span>
<span class="line">php71</span>
<span class="line">php72</span>
<span class="line">php73</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="web-服务器" tabindex="-1"><a class="header-anchor" href="#web-服务器"><span>Web 服务器</span></a></h3>
<p>默认情况下，Homestead 使用 Nginx 作为 Web 服务器。不过，如果指定 <code v-pre>apache</code> 作为站点类型，也能安装 Apache。两个 Web 服务器可以同时安装，但是不能同时运行。Shell 命令 <code v-pre>flip</code> 可用于简化在不同 Web 服务器之间的切换处理，其工作原理是先自动判断哪个 Web 服务器正在运行，然后将其关闭，接着启动另一个服务器。要使用这个命令，需要 SSH 进入到你的到 Homestead 虚拟机并在终端中运行：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">flip</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="邮件" tabindex="-1"><a class="header-anchor" href="#邮件"><span>邮件</span></a></h3>
<p>Homestead 包含了 Postfix 邮件转发代理，默认监听 <code v-pre>1025</code> 端口，所以，你可以指定你的应用在 <code v-pre>localhost</code> 端口 <code v-pre>1025</code> 上使用 <code v-pre>smtp</code> 邮件驱动。这样，所有发送的邮件将由 Postfix 处理并由 Mailhog 捕获。要查看已发送的邮件，可以在浏览器打开 <code v-pre>http://localhost:8025/</code>。</p>
<h2 id="调试-分析" tabindex="-1"><a class="header-anchor" href="#调试-分析"><span>调试 &amp; 分析</span></a></h2>
<h3 id="使用-xdebug-调试-web-请求" tabindex="-1"><a class="header-anchor" href="#使用-xdebug-调试-web-请求"><span>使用 Xdebug 调试 Web 请求</span></a></h3>
<p>Homestead 引入了 <a href="https://xdebug.org/" target="_blank" rel="noopener noreferrer">Xdebug</a> 提供对逐步调试的支持。例如，你可以从浏览器加载某个页面，此时 PHP 会连接到你的 IDE 以便允许对运行代码进行检查和修改。</p>
<p>默认情况下，Xdebug 已经运行并且准备好接收连接，如果你需要在 CLI 中启用 Xdebug，在 Homestead 虚拟机中运行 <code v-pre>sudo phpenmod xdebug</code> 命令即可。接下来，按照 IDE 的引导来启用调试功能。最后，通过浏览器扩展或者 <a href="https://www.jetbrains.com/phpstorm/marklets/" target="_blank" rel="noopener noreferrer">bookmarklet</a> 配置浏览器触发 Xdebug。</p>
<blockquote>
<p>注意：Xdebug 会导致 PHP 运行变慢，要禁用 Xdebug，可以在虚拟机中运行 <code v-pre>sudo phpdismod xdebug</code> 并重启 PHP-FPM 服务。</p>
</blockquote>
<h3 id="调试-cli-应用" tabindex="-1"><a class="header-anchor" href="#调试-cli-应用"><span>调试 CLI 应用</span></a></h3>
<p>要调试 PHP CLI 应用，可以在 Homestead 虚拟机中使用 <code v-pre>xphp</code> 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">xphp path/to/script</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="自动启动-xdebug" tabindex="-1"><a class="header-anchor" href="#自动启动-xdebug"><span>自动启动 Xdebug</span></a></h4>
<p>当发送请求到 Web 服务器调试功能测试时，自动启动调试比修改测试通过自定义请求头或者 Cookie 来触发调试要轻松许多。要设置 Xdebug 自动启动，可以在 Homestead 虚拟机中编辑 <code v-pre>/etc/php/7.#/fpm/conf.d/20-xdebug.ini</code> 添加如下配置来完成：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token punctuation">;</span> If Homestead.yml contains a different subnet <span class="token keyword">for</span> the IP address, this address may be different<span class="token punctuation">..</span>.</span>
<span class="line">xdebug.remote_host <span class="token operator">=</span> <span class="token number">192.168</span>.10.1</span>
<span class="line">xdebug.remote_autostart <span class="token operator">=</span> <span class="token number">1</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用-blackfire-分析应用" tabindex="-1"><a class="header-anchor" href="#使用-blackfire-分析应用"><span>使用 Blackfire 分析应用</span></a></h3>
<p><a href="https://blackfire.io/docs/introduction" target="_blank" rel="noopener noreferrer">Blackfire</a> 是一个用于分析 Web 请求和 CLI 应用以及编写性能断言的 Saas 服务，它提供了一个交互式用户界面用来显示调用关系图和时间线中的分析数据，可以在开发、预发和生产环境中使用，对终端用户来说也没有额外开销，我们可以通过它实现对代码和 <code v-pre>php.ini</code> 配置的性能、质量和安全检查。</p>
<p><a href="https://blackfire.io/docs/player/index" target="_blank" rel="noopener noreferrer">Blackfire Player</a> 是一个开源的功能强大的 Web 爬取、测试和数据提取应用，可以和 Blackfire 一起用于脚本分析场景。</p>
<p>要启用 Blackfire，需使用 Homestead 配置文件的 <code v-pre>features</code> 配置项来设置：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">features:</span>
<span class="line">    - blackfire:</span>
<span class="line">        server_id: <span class="token string">"server_id"</span></span>
<span class="line">        server_token: <span class="token string">"server_value"</span></span>
<span class="line">        client_id: <span class="token string">"client_id"</span></span>
<span class="line">        client_token: <span class="token string">"client_value"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>获取Blackfire 服务器凭证和客户端凭证<a href="https://blackfire.io/signup" target="_blank" rel="noopener noreferrer">需要注册账号</a>，Blackfire 提供了多个方案来分析应用，包括一个客户端工具和浏览器扩展，更多详细请参考 <a href="https://blackfire.io/docs/cookbooks/index" target="_blank" rel="noopener noreferrer">Blackfire 官方文档</a>。</p>
<h3 id="使用-xhgui-分析-php-性能" tabindex="-1"><a class="header-anchor" href="#使用-xhgui-分析-php-性能"><span>使用 XHGui 分析 PHP 性能</span></a></h3>
<p><a href="https://www.github.com/perftools/xhgui" target="_blank" rel="noopener noreferrer">XHGui</a> 是一个用于探索 PHP 应用性能的图形化界面，需要结合 XHProf 使用。要启用 XHGui，需在站点配置中新增 <code v-pre>xhgui: 'true'</code> 配置：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">sites:</span>
<span class="line">    -</span>
<span class="line">        map: your-site.test</span>
<span class="line">        to: /home/vagrant/your-site/public</span>
<span class="line">        type: <span class="token string">"apache"</span></span>
<span class="line">        xhgui: <span class="token string">'true'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果站点已经存在，更新配置之后需要运行 <code v-pre>vagrant provision</code> 命令使其生效。</p>
<p>要分析一个 Web 请求，需要添加 <code v-pre>xhgui=on</code> 到查询字符串作为请求参数，XHGui 会自动添加 Cookie 到响应，以便后续请求不再需要添加查询字符串。你可以访问 <code v-pre>http://your-site.test/xhgui</code> 来查看应用分析结果。</p>
<p>要使用 XHGui 分析一个 CLI 请求，需要在命令之前加上 <code v-pre>XHGUI=on</code> 前缀：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token assign-left variable">XHGUI</span><span class="token operator">=</span>on path/to/script</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>CLI 分析结果可以通过和 Web 分析结果一样的方式查看。</p>
<p>注意以上分析行为会让脚本执行变慢，甚至会导致性能降低一半，因此，我们通常会按照百分比选取一小部分样本进行分析，并根据分析结果进行改进，此外，注意分析结果中的执行时间包含了调试器中花费的所有时间。</p>
<p>由于性能分析会耗费大量的磁盘空间，它们会在几天之后被自动删除。</p>
<h2 id="网络接口" tabindex="-1"><a class="header-anchor" href="#网络接口"><span>网络接口</span></a></h2>
<p><code v-pre>Homestead.yaml</code> 中的 <code v-pre>networks</code> 属性为 Homestead 环境配置网络接口。你可以根据需要配置多个接口：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">networks:</span>
<span class="line">    - type: <span class="token string">"private_network"</span></span>
<span class="line">      ip: <span class="token string">"192.168.10.20"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要开启一个 <a href="https://www.vagrantup.com/docs/networking/public_network.html" target="_blank" rel="noopener noreferrer">桥接</a> 接口，需配置 <code v-pre>bridge</code> 设置并修改网络类型为 <code v-pre>public_network</code> ：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">networks:</span>
<span class="line">    - type: <span class="token string">"public_network"</span></span>
<span class="line">      ip: <span class="token string">"192.168.10.20"</span></span>
<span class="line">      bridge: <span class="token string">"en1: Wi-Fi (AirPort)"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要开启<a href="https://www.vagrantup.com/docs/networking/public_network.html" target="_blank" rel="noopener noreferrer">DHCP</a>，只需要从配置中移除 <code v-pre>ip</code> 选项即可：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">    networks:</span>
<span class="line">        - type: <span class="token string">"public_network"</span></span>
<span class="line">          bridge: <span class="token string">"en1: Wi-Fi (AirPort)"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="扩展-homestead" tabindex="-1"><a class="header-anchor" href="#扩展-homestead"><span>扩展 Homestead</span></a></h2>
<p>你可以使用 Homestead 根目录下的 <code v-pre>after.sh</code> 脚本来扩展 Homestead 的功能，在这个文件中，你可以添加任意 shell 命令来配置和自定义你的虚拟机。</p>
<p>当自定义 Homestead 时，Ubuntu 可能会询问你是要保留程序包的原始配置还是用一个新的配置文件覆盖它。为了避免这种情况，你应该在安装软件包时使用以下的命令，以避免覆盖之前由 Homestead 编写的任何配置：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token parameter variable">-y</span> <span class="token punctuation">\</span></span>
<span class="line">    <span class="token parameter variable">-o</span> Dpkg::Options::<span class="token operator">=</span><span class="token string">"--force-confdef"</span> <span class="token punctuation">\</span></span>
<span class="line">    <span class="token parameter variable">-o</span> Dpkg::Options::<span class="token operator">=</span><span class="token string">"--force-confold"</span> <span class="token punctuation">\</span></span>
<span class="line">    <span class="token function">install</span> your-package</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="用户自定义" tabindex="-1"><a class="header-anchor" href="#用户自定义"><span>用户自定义</span></a></h3>
<p>在团队环境中使用 Homestead 时，你可能想要调整 Homestead 以便更好地适应你自己喜好的个人开发风格，这可以通过在 <code v-pre>Homestead</code> 根目录下创建 <code v-pre>user-customizations.sh</code> 文件来完成，在这个文件中，你可以做一些自己喜欢的定制化工作；不过，注意不要把 <code v-pre>user-customizations.sh</code> 文件提交到代码仓库中。</p>
<h2 id="更新-homestead" tabindex="-1"><a class="header-anchor" href="#更新-homestead"><span>更新 Homestead</span></a></h2>
<p>更新 Homestead 之前确保你已经在 Homestead 目录下通过如下命令移除了当前的虚拟机：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">vagrant destroy</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，需要更新 Homestead 源码，如果你已经克隆仓库到本地，可以在项目根目录下运行如下命令进行更新：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">git</span> fetch</span>
<span class="line"></span>
<span class="line"><span class="token function">git</span> pull origin release</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>这些命令从 GitHub 仓库拉取最新的 Homestead 代码，获取最新的标记，然后检出最新的标记版本。你能在 <a href="https://github.com/laravel/homestead/releases" target="_blank" rel="noopener noreferrer">GitHub 版本页面</a> 找到最新稳定的版本。</p>
<p>如果你通过你的项目的 <code v-pre>composer.json</code> 安装 Homestead，你应当确保你的 <code v-pre>composer.json</code> 包含 <code v-pre>&quot;laravel/homestead&quot;: &quot;^9&quot;</code> 并更新你的依赖项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> update</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>然后，需要通过 <code v-pre>vagrant box update</code> 命令更新 Vagrant 盒子：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">vagrant box update</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>最后，你需要重新生成新的 Homestead 盒子来使用最新安装的 Vagrant：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">vagrant up</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="提供特殊设置" tabindex="-1"><a class="header-anchor" href="#提供特殊设置"><span>提供特殊设置</span></a></h2>
<h3 id="virtualbox" tabindex="-1"><a class="header-anchor" href="#virtualbox"><span>VirtualBox</span></a></h3>
<h4 id="natdnshostresolver" tabindex="-1"><a class="header-anchor" href="#natdnshostresolver"><span><code v-pre>natdnshostresolver</code></span></a></h4>
<p>默认情况下，Homestead 将 <code v-pre>natdnshostresolver</code> 配置设置为 <code v-pre>on</code>。这允许 Homestead 去使用你的主机操作系统的 DNS 设置。如果你不想覆盖这个行为，添加如下的行到你的 <code v-pre>Homestead.yaml</code> 文件：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">provider: virtualbox</span>
<span class="line">natdnshostresolver: off</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="windows-上的符号链接" tabindex="-1"><a class="header-anchor" href="#windows-上的符号链接"><span>Windows 上的符号链接</span></a></h4>
<p>如果符号链接在你的 Windows 机器上无法正常工作，你可能需要添加以下的块到 <code v-pre>Vagrantfile</code> 文件：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">config.vm.provider <span class="token string">"virtualbox"</span> <span class="token keyword">do</span> <span class="token operator">|</span><span class="token function">v</span><span class="token operator">|</span></span>
<span class="line">    v.customize <span class="token punctuation">[</span><span class="token string">"setextradata"</span>, :id, <span class="token string">"VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root"</span>, <span class="token string">"1"</span><span class="token punctuation">]</span></span>
<span class="line">end</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/homestead/5127" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/hom...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/homestead/5127" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/hom...</a></p>
</blockquote>
</div></template>


